﻿using System;
using System.Collections.Generic;
#if DEBUG
using System.Diagnostics;
#endif // #if DEBUG
using System.Linq;
using System.Web;

using FRL.DebtChased.Extensions;
using FRL.DebtChased.Types;

namespace FRL.DebtChased.Repository.PartModels
{

	/// <summary>
	/// Declaration of CollectorAlertsModel class
	/// </summary>
	public class CollectorAlertsModel : BasePartModel
	{

		/////////////////////////////////////////////////////////
		// Nested classes

		public class Element
		{

			/////////////////////////////////////////////////////////
			// Attributes

			internal int m_iAccountId;
			public int AccountId
			{
				get { return m_iAccountId; }
			}

			internal DateTime m_dtTimestamp;
			public string AgeAsText
			{
				get { return m_dtTimestamp.ToCuteSpanString(); }
			}

			internal string m_strDebtorName;
			public string DebtorName
			{
				get { return m_strDebtorName.DotDotDot(20); }
			}

			internal string m_strError;
			public string Error
			{
				get { return m_strError.DotDotDot(40); }
			}

			/////////////////////////////////////////////////////////
			// Construction

			public Element()
			{

				// Initialize member variables

			}

		}

		/////////////////////////////////////////////////////////////
		// Attributes

		private List<Element> m_elements = new List<Element>();
		public List<Element> Elements
		{
			get { return m_elements; }
		}

		/////////////////////////////////////////////////////////////
		// Construction

		public CollectorAlertsModel( DebtChasedEntities ent, int iUserId )
		{

			// Load the accounts
			var results = from an in ent.AccountNotes
						  join a in ent.Accounts on an.AccountId equals a.AccountId
						  where (
							( (iUserId == 0) || (a.AssignedUserId == iUserId) || (a.AssignedManagerId == iUserId) )
							&&
							(an.Error != null)
							&&
							( (an.fdFlags & 0x02) == 0x02 )
						  )
						  select new { an.Timestamp, an.Error, a.AccountId, a.DebtorName };
			foreach (var result in results)
			{

				// Add a new element
				Element el = new Element() 
				{
					m_iAccountId = result.AccountId,
					m_dtTimestamp = result.Timestamp,
					m_strDebtorName = result.DebtorName,
					m_strError = result.Error
				};
				m_elements.Add(el);

			}

		}

	}

}